package com.itsu.spbspider.service.impl;

import com.alibaba.fastjson.JSON;
import com.itsu.spbspider.entity.Crawler;
import com.itsu.spbspider.mapper.CrawlerMapper;
import com.itsu.spbspider.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service("baiduNewsCrawlerDB")
@Slf4j
public class BaiduNewsCrawlerDB implements com.itsu.spbspider.service.Crawler {

    @Resource
    CrawlerMapper crawlerMapper;

    public Map<String, String> initParamsHeaders(String type, Crawler crawler) throws Exception {
        Map<String, String> res = new HashMap<>();
        Map map = null;
        if ("params".equals(type)) {
            map = JSON.parseObject(crawler.getParams());
        } else
            map = JSON.parseObject(crawler.getHeaders());
        if (MapUtils.isEmpty(map))
            map = new HashMap();
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String value = (String) map.get(key);
            res.put(key, value);
        }

        return res;

    }

    @Override
    public String crawlerData(Integer crawlId) throws Exception {
        Crawler crawler = crawlerMapper.selectById(crawlId);
        Map<String, String> params = initParamsHeaders("params", crawler);
        Map<String, String> headers = initParamsHeaders("headers", crawler);
        String method = crawler.getMethod();
        if (StringUtils.isBlank(method)) {
            method = "post";
        }
        String content = null;
        if ("post".equals(method.toLowerCase())) {
            content = HttpUtil.post(crawler.getUrl(), params, headers);
        } else {
            content = HttpUtil.get(crawler.getUrl(), params, headers);
        }
        log.info("get content baidu news : {}", content);
        return content;
    }

    @Override
    public String crawlerData(Crawler crawler) throws Exception {
        String content = null;
        if (crawler != null) {
            if ("post".equals(crawler.getMethod())) {
                content = HttpUtil.post(crawler.getUrl(), initParamsHeaders(crawler.getParams()), initParamsHeaders(crawler.getHeaders()));
            } else {
                content = HttpUtil.get(crawler.getUrl(), initParamsHeaders(crawler.getParams()), initParamsHeaders(crawler.getHeaders()));
            }
        }
        log.info("get content baidu news : {}", content);
        return content;
    }

    public Map<String, String> initParamsHeaders(String content) {
        Map<String, String> res = new HashMap<>();
        if (StringUtils.isBlank(content)) {
            return res;
        }
        Map map = null;
        map = JSON.parseObject(content);
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            String value = (String) map.get(key);
            res.put(key, value);
        }
        return res;
    }
}
